package com.dmapp.utils;

import java.io.IOException;
import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.res.AssetManager;

public class UserService {

	public static User getUser() {
		return user;
	}

	public static void setUser(User user) {
		UserService.user = user;
	}

	public static User user;

	/**
	 * 判断用户是否登录
	 * 
	 * @author zhongyou.chen
	 * @date 2013-5-15
	 * @return
	 */
	public static boolean isLogin() {
		return user == null ? false : true;
	}

	/**
	 * 用户登录
	 * 
	 * @author zhongyou.chen
	 * @date 2013-5-15
	 * @param userName
	 * @param password
	 * @return
	 */
	public static boolean login(Activity activity, String userName,
			String password,boolean isRemember) {
		AssetManager am = null;
		am = activity.getAssets();
		try {
			InputStream is = am.open("login.json");
			int length = is.available();
			byte[] buffer = new byte[length];
			is.read(buffer);
			String res = new String(buffer);
			System.out.println(res);
			String jsonString = res.substring(res.indexOf("{"));

			try {
				JSONObject jsonObject = new JSONObject(jsonString.toString())
						.getJSONObject("userInfo");
				user = new User();
				user.setPassword(jsonObject.getString("password"));
				user.setUserName(jsonObject.getString("userName"));
				user.setUserId(jsonObject.getString("userId"));
				user.setBalance(jsonObject.getString("balance"));
				user.setIntegral(jsonObject.getString("integral"));
				setUser(user);
				if(isRemember){
					Utils1.saveUserInfo(activity, userName, password, isRemember);
				}else{
					Utils1.saveUserInfo(activity, "", "", isRemember);
				}
				return true;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 用户注册
	 * 
	 * @author zhongyou.chen
	 * @date 2013-5-15
	 * @param userName
	 * @param password
	 * @param email
	 */
	public static boolean register(Activity activity, String userName,
			String password, String email) {
		AssetManager am = null;
		am = activity.getAssets();
		try {
			InputStream is = am.open("register.json");
			int length = is.available();
			byte[] buffer = new byte[length];
			is.read(buffer);
			String res = new String(buffer);
			System.out.println(res);
			String jsonString = res.substring(res.indexOf("{"));
			try {
				JSONObject jsonObject = new JSONObject(jsonString.toString())
						.getJSONObject("userInfo");
				user = new User();
				user.setPassword(jsonObject.getString("password"));
				user.setUserName(jsonObject.getString("userName"));
				user.setUserId(jsonObject.getString("userId"));
				user.setBalance(jsonObject.getString("balance"));
				user.setIntegral(jsonObject.getString("integral"));
				setUser(user);
				return true;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 注销
	 * 
	 * @author zhongyou.chen
	 * @date 2013-5-15
	 */
	public static void exit() {
		user = null;
	}

	/**
	 * 找回密码
	 * 
	 * @author zhongyou.chen
	 * @date 2013-5-15
	 * @param email
	 */
	public static void findPassword(String email) {

	}

	
	

	
	
}
